/* function identity(value: string): string;
function identity(value: number): number;
function identity(value: boolean): boolean;
function identity(value: boolean[]): boolean[];
function identity(value: number[]): number[];
function identity(value: object): object;
function identity(value: string | number | boolean | number[] | boolean[] | object) {
  return value;
}

const s = identity({ id: 1, name: 'John' });
console.log(s.name) // error */

function identity<T>(value: T): T { 
  return value
}

type User = {
  id: number,
  name:string
}

let user: User = {
  id: 1,
  name:"John"
}

// const s1 = identity<string>("hello")
// const s2 = identity<number>(123)
// const s3 = identity<User>(user)
// console.log(s3.name)

const s1 = identity("hello")
const s2 = identity(123)
const s3 = identity(user)
console.log(s3.name)

// 传入相同类型的两个参数，得到这个类型的数组
function getArray<ACC>(a: ACC, b: ACC) { 
  return [a, b]
}
const as = getArray<string>("a", "b");

function myNumberFilter(arr:number[],callback:(item:number, index?:number) => boolean):number[] { 
  const result = [];
  for (let i = 0; i < arr.length; i++) {
    const item = arr[i];
    if (callback(item)) {
      result.push(item);
    }
  }
  return result;
}
const filterArr1 = myNumberFilter([1, 2, 3, 4, 5], item => item % 2 === 0);
console.log(filterArr1);

// function filter<T>(arr: T[], callback: (item: T, index?: number) => boolean) { 
//   const result = [];
//   for (let i = 0; i < arr.length; i++) {
//     const item = arr[i];
//     if (callback(item)) {
//       result.push(item);
//     }
//   }
//   return result;
// }

const filter = <T>(arr: T[], callback: (item: T, index?: number) => boolean): T[] => { 
  const result = [];
  for (let i = 0; i < arr.length; i++) {
    const item = arr[i];
    if (callback(item)) {
      result.push(item);
    }
  }
  return result;
}

const filterArr2 = filter([1, 2, 3, 4, 5], item => item % 2 === 0);
console.log(filterArr2);

const filterArr3 = filter(["xxx.js","aaa.java","bbb.md"], item => item.endsWith(".js"));
console.log(filterArr3);